package com.xnyzc.lhy.mis.service.impl.user.wallet;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.penum.amount.EIsAmount;
import com.xnyzc.lhy.common.penum.order.ETabType;
import com.xnyzc.lhy.common.penum.tab.EIsEnable;
import com.xnyzc.lhy.common.penum.tab.ETab;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.mis.mapper.user.wallet.OaCWalletMapper;
import com.xnyzc.lhy.mis.service.user.wallet.IOaCWalletService;
import com.xnyzc.lhy.mis.entity.dictionaries.OaSysDict;
import com.xnyzc.lhy.mis.entity.user.tab.OaTabPermission;
import com.xnyzc.lhy.mis.entity.user.tab.OaTabUser;
import com.xnyzc.lhy.mis.entity.user.wallet.OaCWallet;
import com.xnyzc.lhy.mis.mapper.dictionaries.OaSysDictMapper;
import com.xnyzc.lhy.mis.mapper.user.tab.OaTabUserMapper;
import com.xnyzc.lhy.mis.mapper.user.wallet.OaCWalletMapper;
import com.xnyzc.lhy.mis.service.user.wallet.IOaCWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户钱包 服务实现类
 * </p>
 *
 * @author zhuyubin
 * @since 2019-08-09
 */
@Service
public class OaCWalletServiceImpl extends ServiceImpl<OaCWalletMapper, OaCWallet> implements IOaCWalletService {

    @Autowired
    private OaCWalletMapper oaCWalletMapper;
    @Autowired
    private OaSysDictMapper oaSysDictMapper;
    @Autowired
    private OaTabUserMapper oaTabUserMapper;

    private static final String DIC_TYPE = "top_up_list";

    /**
     * 获取用户钱包信息
     * @param userId 用户id
     * @param tag 租户
     * @return
     */
    @Override
    public Map<String, Object> getWallet(Long userId, String tag, Integer userType){
        Map<String, Object> result = this.initWallet();
        OaCWallet oaCWallet = oaCWalletMapper.selectOne(
                Wrappers.<OaCWallet>lambdaQuery()
                        .eq(OaCWallet::getUserId, userId)
                        .eq(OaCWallet::getTag, tag)
        );
        if (CheckUtil.objIsEmpty(oaCWallet)){
            return result;
        }
        result.put("myFund", oaCWallet.getWalletBalance().toString());
        result.put("officialLimit", oaCWallet.getDeptOfficialBalance().toString());
        result.put("myLimit", oaCWallet.getPersonalOfficialBalance().toString());
        result.put("isDeptAmount", EIsAmount.no.getValue());
        result.put("isPersonalAmount", EIsAmount.no.getValue());
        List<OaTabUser> oaTabUsers = oaTabUserMapper.selectList(Wrappers.<OaTabUser>lambdaQuery().eq(OaTabUser::getTag, tag).eq(OaTabUser::getUserId, userId).eq(OaTabUser::getUserType, userType).in(OaTabUser::getTabId, ETab.official.getValue(), ETab.dept.getValue()));
        for (OaTabUser oaTabUser : oaTabUsers) {
            if (ETab.official.getValue().equals(oaTabUser.getTabId())){
                result.put("isPersonalAmount", EIsAmount.yes.getValue());
                continue;
            }
            if (ETab.dept.getValue().equals(oaTabUser.getTabId())){
                result.put("isDeptAmount", EIsAmount.yes.getValue());
            }
        }
        return result;
    }

    /**
     * 获取充值列表
     * @return
     */
    @Override
    public List getTopUpList(String tag){
        List result = new ArrayList();
        List<OaSysDict> oaSysDicts = oaSysDictMapper.selectList(
                Wrappers.<OaSysDict>lambdaQuery()
                        .eq(OaSysDict::getType, DIC_TYPE)
                        .eq(OaSysDict::getTag, tag)
        );
        for (OaSysDict oaSysDict : oaSysDicts) {
            Map<String, Object> map = new HashMap<>(16);
            map.put("money", oaSysDict.getLabel());
            result.add(map);
        }
        return result;
    }

    /**
     * 初始化出参数据
     * @return
     */
    private Map<String, Object> initWallet(){
        Map<String, Object> map = new HashMap<>(16);
        map.put("myFund", "0.00");
        map.put("officialLimit", "0.00");
        map.put("isDeptAmount", 0);
        map.put("myLimit", "0.00");
        map.put("isPersonalAmount", 0);
        return map;
    }

    /**
     * 根据用户id获取公务额度以及其他信息
     * @param userId 用户id
     * @return # 个人公务额度 部门公务额度 用车的可用时间 用车的可用车型 用车的可用地点 是否显示部门可用额度
     */
//    @Override
//    public Map<String,Object> getOfficialAmountByUserId(Long userId) {
//        Map<String, Object> map = new HashMap<>(16);
//        map.put("personalOfficialMoney", "0.0");
//        map.put("departmentOfficialMoney", "0.0");
//        map.put("ifShowAvailableMoney", "");
//        map.put("availableVehicleType", "");
//        map.put("availableTime", "");
//        map.put("availablePlace", "");
//        OaCWallet oaCWallet = oaCWalletMapper.selectOne(Wrappers.<OaCWallet>lambdaQuery().eq(OaCWallet::getUserId, userId));
//        if (oaCWallet == null){
//            return map;
//        }
//        map.put("personalOfficialMoney", oaCWallet.getPersonalOfficialBalance());
//        map.put("departmentOfficialMoney", oaCWallet.getDeptOfficialBalance());
//        map.put("ifShowAvailableMoney", EIsDepartmentMoneyDisplayType.findById(oaCWallet.getIsDeptAmount()).getValue());
//        Integer deptId = oaCWallet.getDeptId();
//        if (deptId == null){
//            return map;
//        }
//        OaCUserMode oaCUserMode = oaCUserModeMapper.selectOne(Wrappers.<OaCUserMode>lambdaQuery().eq(OaCUserMode::getDeptId, deptId));
//        if (oaCUserMode != null){
//            map.put("availableVehicleType", oaCUserMode.getAvailableModel());
//            map.put("availableTime", EUsableTimeType.findById(oaCUserMode.getAvailableTime()).getValue());
//            map.put("availablePlace", oaCUserMode.getAvailableCity());
//        }
//        return map;
//    }
}
